package com.zhu.androidsample.business.girls;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;

import com.zhu.androidsample.R;
import com.zhu.androidsample.business.girls.adpter.GirlAdapter;
import com.zhu.androidsample.business.girls.presenter.GirlPresenter;
import com.zhu.androidsample.common.BaseActivity;
import com.zhu.androidsample.common.BaseAdapter;

import java.util.ArrayList;

public class GirlActivity extends BaseActivity {


    ////////////////////////////////约定区////////////////////////////////////////
    /**
     * 更新页面的号码归属地信息
     **/
    public static final int SET_DATA = 0x010009;
    public static final int APPEND_DATA = 0x010008;

    ////////////////////////////////view区////////////////////////////////////////
    RecyclerView recyclerView;
    BaseAdapter adapter;
    RecyclerView.LayoutManager layoutManager;
    SwipeRefreshLayout swipeRefreshLayout;

    GirlPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_girl);

        recyclerView = (RecyclerView) findViewById(R.id.girl_list);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.girl_swiperefreshlayout);
        init();
    }

    private void init() {
        layoutManager = new LinearLayoutManager(this);
        adapter = new GirlAdapter();
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));
        presenter = new GirlPresenter(this);
        presenter.initData();
        swipeRefreshLayout.setOnRefreshListener(presenter);
    }

    ////////////////////////////////响应区////////////////////////////////////////
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case SET_DATA:
                this.setData((ArrayList) msg.obj);
                break;
            case APPEND_DATA:
                this.appendData((ArrayList) msg.obj);
                break;
        }
        return super.handleMessage(msg);
    }

    public void setData(ArrayList list) {
        adapter.setData(list);
        recyclerView.swapAdapter(adapter,false);
        swipeRefreshLayout.setRefreshing(false);

    }

    public void appendData(ArrayList list) {
        adapter.appendData(list);
        swipeRefreshLayout.setRefreshing(false);
    }
}
